﻿#region Copyright (C) 2008, 2009 StreamTv

/* 
 *      Copyright (C) 2008, 2009 StreamTv
 *      http://code.google.com/p/mpstreamtv/
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#endregion

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.IO.Pipes;

namespace TimeShifting.Media.Encoders
{
    public abstract class ProcessWrapper : Stream
    {
        // Transport Pipes
        private TransportStream encoderInput;
        private TransportStream encoderOutput;

        // Media
        private Stream media;
        private String mediaUrl;

        public ProcessWrapper(Stream input, TransportMethod inputMethod, TransportMethod outputMethod)
        {
            SetupPipes(inputMethod, outputMethod);

            media = input;
        }

        public ProcessWrapper(String url, TransportMethod inputMethod, TransportMethod outputMethod)
        {
            SetupPipes(inputMethod, outputMethod);

            mediaUrl = url;
        }

        private void SetupPipes(TransportMethod inputMethod, TransportMethod outputMethod)
        {
            switch (inputMethod)
            {
                case TransportMethod.UDP:
                    throw new NotImplementedException("UDP transport method not implemented.");
                case TransportMethod.TCP:
                    throw new NotImplementedException("TCP transport method not implemented.");
                case TransportMethod.NamedPipe:
                    encoderInput = new Transport.NamedPipe();
                    break;
                case TransportMethod.HTTP:
                    throw new NotImplementedException("HTTP transport method not implemented.");
                case TransportMethod.RTSP:
                    throw new NotImplementedException("RTSP transport method not implemented.");
                default:
                    throw new ArgumentException("Invalid option.");
            }

            switch (outputMethod)
            {
                case TransportMethod.UDP:
                    throw new NotImplementedException("UDP transport method not implemented.");
                case TransportMethod.TCP:
                    throw new NotImplementedException("TCP transport method not implemented.");
                case TransportMethod.NamedPipe:
                    encoderOutput = new Transport.NamedPipe();
                    break;
                case TransportMethod.HTTP:
                    throw new NotImplementedException("HTTP transport method not implemented.");
                case TransportMethod.RTSP:
                    throw new NotImplementedException("RTSP transport method not implemented.");
                default:
                    throw new ArgumentException("Invalid option.");
            }
        }

        protected void Start()
        {
            if (media != null)
                StartPipe(media);
            else if (mediaUrl != null)
                StartPipe(mediaUrl);
            else
                throw new ArgumentNullException("Input media is set to null.");
        }

        private void StartPipe(Stream media)
        {
            encoderInput.Start(false);

            // Start the transcoder.
            StartProcess(encoderInput.Url, encoderOutput.Url);

            encoderInput.CopyStream(media);

            encoderOutput.Start(false);

            // Wait for the output encoder to connect.
            int tries = 10000;

            do
            {
                if (encoderOutput.IsReady)
                    break;

                System.Threading.Thread.Sleep(1);
            } while (--tries != 0);
        }

        private void StartPipe(String media)
        {
            // Start the transcoder.
            if (media.StartsWith("\""))
            {
                StartProcess(media, encoderOutput.Url);
            }
            else
            {
                StartProcess(String.Format("\"{0}\"", media), encoderOutput.Url);
            }

            encoderOutput.Start(false);
        }

        public override bool CanRead
        {
            get
            {
                return encoderOutput.CanRead;
            }
        }

        public override bool CanSeek
        {
            get
            {
                return encoderOutput.CanSeek;
            }
        }

        public override bool CanWrite
        {
            get { throw new NotSupportedException(); }
        }

        public override void Flush()
        {
            throw new NotSupportedException();
        }

        public override long Length
        {
            get
            {
                return encoderOutput.Length;
            }
        }

        public override long Position
        {
            get
            {
                return encoderOutput.Position;
            }
            set
            {
                encoderOutput.Position = value;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return encoderOutput.Read(buffer, offset, count);
        }

        public override long Seek(long offset, SeekOrigin origin)
        {
            return encoderOutput.Seek(offset, origin);
        }

        public override void SetLength(long value)
        {
            encoderOutput.SetLength(value);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new NotSupportedException();
        }

        public abstract void StartProcess(String input, String output);

        public abstract void StopProcess();
    }
}
